package cn.com.mrzheng.web.controller.base;

import cn.com.mrzheng.web.server.Base.BaseServiceImpl;
import cn.com.mrzheng.web.utils.Envelope;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Map;

public class BaseController<T,J extends BaseServiceImpl> {

    /**
     * Api 地址必须是按照规范来写否则报错
     * Api静态类的类名为控制器名称 除去 Controller
     * 举例  UserController 这 Api静态类名为 User
     * 该类封装 增删该查 因此具体 api方法常量名也是固定
     * 举例  LIST ， SAVE ， DELETE， GET
     * */
    @Autowired
    J Server;

    // 获取列表
    public Envelope list(String filter, String currentPage, String pageSize){
        Envelope<T> envelope = new Envelope<>();
        List<T> list = null;
        List<T> total = null;
        try {
            list = Server.list(filter, currentPage, pageSize);
            total = Server.list(filter, "", "");
            envelope.setData(list);
            envelope.setTotal(total.size());
            envelope.setCurrentSize(list.size());
            envelope.setSuccessFlg(true);
        }catch (Exception e){
            envelope.setSuccessFlg(false);
            envelope.setErrorMsg(e.getMessage());
            e.printStackTrace();
        }
        return envelope;
    }

    // 保存
    public Envelope saveEntity(String datajson){
        Envelope<T> envelope = new Envelope<>();
        try {
            int State = Server.saveEntity(datajson);
            stateToEnvelope(State,envelope);
        }catch (Exception e) {
            envelope.setSuccessFlg(false);
            envelope.setErrorMsg(e.getMessage());
            e.printStackTrace();
        }
        return envelope;
    }

    // 保存 唯一数据库值
    public Envelope saveEntity(String datajson,String uniqueJson){
        Envelope<T> envelope = new Envelope<>();
        try {
            int State = Server.saveEntity(datajson,uniqueJson);
            stateToEnvelope(State,envelope);
        }catch (Exception e) {
            envelope.setSuccessFlg(false);
            envelope.setErrorMsg(e.getMessage());
            e.printStackTrace();
        }
        return envelope;
    }

    // 删除
    public Envelope deleteEntity(String id){
        Envelope<T> envelope = new Envelope<>();
        try {
            int State =  Server.deleteEntity(id);
            stateToEnvelope(State,envelope);
        }catch (Exception e) {
            envelope.setSuccessFlg(false);
            envelope.setErrorMsg(e.getMessage());
            e.printStackTrace();
        }
        return envelope;
    }

    public static void stateToEnvelope (Integer State, Envelope envelope) {
        switch (State){
            case 1: {
                envelope.setSuccessFlg(true);
                break;
            }
            case 0: {
                envelope.setSuccessFlg(false);
                envelope.setErrorMsg("数据库操作错误");
                break;
            }
            case 404: {
                envelope.setSuccessFlg(false);
                envelope.setErrorMsg("数据重复");
                break;
            }
        }
    }

}
